package com.cskaoyan.service.admin;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.CharUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author stone
 * @date 2022/06/25 11:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketAddressMapper addressMapper;

    @Autowired
    MarketCollectMapper collectMapper;

    @Autowired
    MarketFootprintMapper footprintMapper;

    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Autowired
    MarketFeedbackMapper feedbackMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Override
    public CommonData<MarketUser> query(String username, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike(username);
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);

        return CommonData.data(pageInfo);
    }

    // TODO 会员管理
    // 会员管理模块显示、查询
    @Override
    public CommonData<MarketUser> list(BasePageInfo basePageInfo, String username, String mobile) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        // 下面只根据username和mobile做条件查询,id是另外的接口
        MarketUserExample example = new MarketUserExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        // 创建一个criteria用来拼接sql语句条件
        MarketUserExample.Criteria criteria = example.createCriteria();
        // 如果存在username和mobile则表示为查询操作
        if (!StringUtils.isEmpty(username)) {
            // 模糊查询
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<MarketUser> list = marketUserMapper.selectByExample(example);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketUser queryUserById(Integer id) {
        // 这里是根据id查询
        MarketUserExample example = new MarketUserExample();
        // 创建一个criteria用来拼接sql语句条件
        MarketUserExample.Criteria criteria = example.createCriteria();
        // 查询id
        criteria.andIdEqualTo(id);
        MarketUser userData = marketUserMapper.selectByPrimaryKey(id);
        return userData;
    }

    // 修改会员用户信息
    @Override
    public int updateUser(MarketUser user) {
        int data = marketUserMapper.updateByPrimaryKey(user);
        return data;
    }

    // TODO 收货地址
    // 收货地址模块显示、查询
    @Override
    public CommonData<MarketAddress> addressList(BasePageInfo basePageInfo, String name, Integer userId) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        // 下面只根据name和userId做条件查询
        MarketAddressExample example = new MarketAddressExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 创建一个criteria用来拼接sql语句条件
        MarketAddressExample.Criteria criteria = example.createCriteria();
        // 如果存在name和userId则表示为查询操作
        if (!StringUtils.isEmpty(name)) {
            // 模糊查询
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> list = addressMapper.selectByExample(example);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    // TODO 会员收藏
    // 会员收藏模块显示、查询
    @Override
    public CommonData<MarketCollect> collectList(BasePageInfo basePageInfo, Integer userId, Integer valueId) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        // 下面是根据userId和valueId做条件查询
        MarketCollectExample example = new MarketCollectExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 创建一个criteria用来拼接sql语句条件
        MarketCollectExample.Criteria criteria = example.createCriteria();
        // 如果存在userId和valueId则表示为查询操作
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> list = collectMapper.selectByExample(example);
        PageInfo<MarketCollect> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    //TODO 会员足迹
    @Override
    public CommonData<MarketFootprint> footprintList(BasePageInfo basePageInfo, Integer userId, Integer valueGoodsId) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketFootprintExample example = new MarketFootprintExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 创建一个criteria用来拼接sql语句条件
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueGoodsId != null) {
            criteria.andGoodsIdEqualTo(valueGoodsId);
        }
        List<MarketFootprint> list = footprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    // TODO 搜索历史
    @Override
    public CommonData<MarketSearchHistory> searchHistoryList(BasePageInfo basePageInfo, Integer userId, String keyword) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 创建一个criteria用来拼接sql语句条件
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<MarketSearchHistory> list = searchHistoryMapper.selectByExample(example);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFeedback> feedbackList(BasePageInfo basePageInfo, String username, Integer id) {
        // 分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketFeedbackExample example = new MarketFeedbackExample();
        // 排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // 创建一个criteria用来拼接sql语句条件
        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> list = feedbackMapper.selectByExample(example);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    // 微信小程序订单状态查询
    @Override
    public WxUserOrder queryOrder() {
        Subject subject = SecurityUtils.getSubject();


        WxUserOrder wxUserOrder = new WxUserOrder();
        if (subject.isAuthenticated()) {
            UserOrder userOrder = new UserOrder();
            MarketUser user = (MarketUser)subject.getPrincipal();
            Integer userId = user.getId();
            Integer unrecv = orderMapper.queryUnNum(userId, 301);
            Integer uncomment = orderMapper.queryUncomment(userId);
            Integer unpid = orderMapper.queryUnNum(userId, 101);
            Integer unship = orderMapper.queryUnNum(userId, 201);
            userOrder.setUnrecv(unrecv);
            userOrder.setUnpaid(unpid);
            userOrder.setUnship(unship);
            userOrder.setUncomment(uncomment);
            wxUserOrder.setOrder(userOrder);
        }

        return wxUserOrder;
    }

    // 注册查询重复用户名
    @Override
    public List<MarketUser> queryUserByUsername(String username) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<MarketUser> list = marketUserMapper.selectByExample(example);
        return list;
    }

    // 注册查询手机号是否注册过
    @Override
    public List<MarketUser> queryUserByMobile(String mobile) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<MarketUser> list = marketUserMapper.selectByExample(example);
        return list;
    }

    // 给新注册的账号添加登录时间
    @Override
    public void add(MarketUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        marketUserMapper.insertSelective(user);
    }
}
